package com.huluobo.a2302mdmdemo.pages

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2302mdmdemo.MyNavHost
import com.huluobo.a2302mdmdemo.R
import com.huluobo.apps.pages.AppsPage
import com.huluobo.document.pages.LocalFilePage
import com.huluobo.lib_base.RoutePath
import com.huluobo.report.pages.ReportPage

/**
 *  Created by LC on 2025/7/22.
 *  首页大框架页面,在MainActivity中不放任何控件,导航之类的东西都在当前page中
 */
//封装首页底部导航的item
sealed class NavItem(val route: String, val title: String, val img: Int) {
    data object MessagePage : NavItem(RoutePath.MESSAGE, "消息", R.drawable.message)
    data object HomePage : NavItem(RoutePath.HOME, "门户", R.drawable.home)
    data object MinePage : NavItem(RoutePath.MINE, "我的", R.drawable.mine)
}

@Composable
fun MainPage() {
    //底部导航的数据源
    val list = listOf(NavItem.MessagePage, NavItem.HomePage, NavItem.MinePage)

    //导航控制器
    val navHostController = rememberNavController()

    var clickName by remember {
        mutableStateOf("门户")
    }

    //拿到导航器的页面回退栈
    val stackEntryState by navHostController.currentBackStackEntryAsState()

    //通过回退栈拿到当前导航中的所有路由地址
    val destination = stackEntryState?.destination

    Scaffold(
        bottomBar = {

            if (destination?.hierarchy?.any {
                    //通过拿到底部导航的地址(历史记录)来对比,如果其中有底部导航3个页面的地址的话
                    list.map { navItem ->
                        // list.map 遍历集合,拿到所有的地址部分
                        navItem.route
                        //contains 是否包含  如果遍历之后的地址包含回退站的地址,就为true,显示底部导航
                    }.contains(it.route)
                } == true) {
                NavigationBar {
                    list.forEach {
                        NavigationBarItem(selected = it.title == clickName, onClick = {
                            clickName = it.title
                            //当点击导航的item时,跳转到对应的路由地址
                            navHostController.navigate(it.route)
                        }, icon = {
                            //加载底部导航数据源中的图标
//                        if (it.title == clickName) {
                            Icon(
                                painter = painterResource(id = it.img),
                                contentDescription = "",
                                modifier = Modifier.size(30.dp)
                            )
//                        }
//                        else {
//                            Icon(imageVector = Icons.Default.Close, contentDescription = "")
//                        }

                        }, label = {
                            Text(text = it.title)
                        })
                    }
                }
            }
        }
    ) {
        MyNavHost(it = it, navHostController = navHostController)
    }
}